package com.avecsys.quotemanager.mbeans.supplier;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.icefaces.ace.component.fileentry.FileEntry;
import org.icefaces.ace.component.fileentry.FileEntryEvent;
import org.icefaces.ace.component.fileentry.FileEntryResults;
import org.icefaces.ace.component.fileentry.FileEntryResults.FileInfo;

import com.avecsys.quotemanager.constants.ApplicationConstants;
import com.avecsys.quotemanager.dao.common.EnumActor;
import com.avecsys.quotemanager.exception.BaseException;
import com.avecsys.quotemanager.mbeans.core.BaseMBean;
import com.avecsys.quotemanager.pojos.QuoteTemplateMasterPojo;
import com.avecsys.quotemanager.pojos.SupplierUsersPojo;
import com.avecsys.quotemanager.pojos.UserMasterPojo;
import com.avecsys.quotemanager.service.supplier.QuoteTemplateMasterService;
import com.avecsys.quotemanager.utils.MessageBoardUtil;
import com.avecsys.quotemanager.utils.TransactionStatus;
import com.avecsys.quotemanager.vo.SupplierVO;
import com.icesoft.faces.component.ext.HtmlInputText;

/**
 * @author Sam Sundar K
 */
public class QuoteTemplateMasterMBean extends BaseMBean implements Serializable
{

	private final Log log = LogFactory.getLog(QuoteTemplateMasterMBean.class);

	private QuoteTemplateMasterService quoteTemplateMasterService;

	private QuoteTemplateMasterPojo quoteTemplateMasterPojo = new QuoteTemplateMasterPojo();

	private List<SelectItem> fileTypeSelectItemList = new ArrayList<SelectItem>();

	private boolean quoteAttachmentRequired = true;

	private boolean modifyExistingQuoteAttachment = false;

	private boolean quoteAttachmentDisabled = true;

	private QuoteTemplateWrapper quoteTemplateWrapper;

	private HtmlInputText widgetTemplateName;

	private HtmlInputText widgetTemplateDescription;

	public QuoteTemplateMasterMBean()
	{
		super();
	}

	public String actionCancel()
	{
		setQuoteTemplateMasterPojo(new QuoteTemplateMasterPojo());
		return "openListSupplierQuoteTemplate";
	}

	public void actionModifyExistingQuoteAttachment(ValueChangeEvent valueChangeEvent)
	{
		setModifyExistingQuoteAttachment((Boolean) valueChangeEvent.getNewValue());
		setQuoteAttachmentRequired((Boolean) valueChangeEvent.getNewValue());
		setQuoteAttachmentDisabled(!(Boolean) valueChangeEvent.getNewValue());
		getWidgetTemplateName().validate(FacesContext.getCurrentInstance());
		getWidgetTemplateDescription().validate(FacesContext.getCurrentInstance());
	}

	public void actionQuoteAttachmentListener(FileEntryEvent fileEntryEvent)
	{
		FileEntry fileEntry = (FileEntry) fileEntryEvent.getComponent();
		FileEntryResults fileEntryResults = fileEntry.getResults();
		List<FileInfo> fileInfoList = fileEntryResults.getFiles();
		for (FileInfo fileInfo : fileInfoList)
		{
			if (fileInfo.isSaved())
			{
				FacesContext facesContext = FacesContext.getCurrentInstance();
				facesContext.getExternalContext()
						.getSessionMap()
						.put(ApplicationConstants.QUOTE_TEMPLATE_ATTACHEMNT_FILE_INFO, fileInfo);
			}
			else
			{

			}
		}
	}

	public String actionSaveQuoteTemplate()
	{
		try
		{
			log.info("Started Saving Quote Template Master Information into database");
			// Step1: Update user modification information
			updateUserInformation(quoteTemplateMasterPojo, null);
			// Step2: Update Logged in user Supplier to Quote
			UserMasterPojo userMasterPojo = getSignedInUser();
			for (SupplierUsersPojo supplierUsersPojo : userMasterPojo.getSupplierUsersPojo())
			{
				quoteTemplateMasterPojo.setSupplierMasterPojo(supplierUsersPojo.getSupplierMasterPojo());
			}
			// Step2: Save Supplier Quote Template Master Information
			// Prepare Value Object
			SupplierVO supplierVO = new SupplierVO();
			FileInfo quoteAttachmentFileInfo = (FileInfo) FacesContext.getCurrentInstance()
					.getExternalContext()
					.getSessionMap()
					.get(ApplicationConstants.QUOTE_TEMPLATE_ATTACHEMNT_FILE_INFO);
			FacesContext.getCurrentInstance()
					.getExternalContext()
					.getSessionMap()
					.remove(ApplicationConstants.QUOTE_TEMPLATE_ATTACHEMNT_FILE_INFO);
			supplierVO.setQuoteAttachmentFileInfo(quoteAttachmentFileInfo);
			supplierVO.setQuoteTemplateMasterPojo(quoteTemplateMasterPojo);
			TransactionStatus transactionStatus = quoteTemplateMasterService.saveQuoteTemplateMaster(supplierVO);
			MessageBoardUtil.publishTransactionStatusMessage(
					transactionStatus,
						EnumActor.QUOTE_TEMPLATE,
						"messageBoardId",
						"",
						new Object[]
						{ quoteTemplateMasterPojo.getTemplateName() });
			log.info("Completed saving Quote Template Master Information into database sucessfully");
			setQuoteTemplateMasterPojo(new QuoteTemplateMasterPojo());
		}
		catch (BaseException e)
		{
			log.error(e);
			MessageBoardUtil.publishException(e);
		}
		catch (Exception e)
		{
			log.error(e);
			MessageBoardUtil.publishException(e);
		}
		return "openQuoteTemplateListScreen";
	}

	/**
	 * @return the fileTypeSelectItemList
	 */
	public List<SelectItem> getFileTypeSelectItemList()
	{
		return fileTypeSelectItemList;
	}

	/**
	 * @return the quoteTemplateMasterPojo
	 */
	public QuoteTemplateMasterPojo getQuoteTemplateMasterPojo()
	{
		return quoteTemplateMasterPojo;
	}

	/**
	 * @return the quoteTemplateMasterService
	 */
	public QuoteTemplateMasterService getQuoteTemplateMasterService()
	{
		return quoteTemplateMasterService;
	}

	/**
	 * @return the quoteTemplateWrapper
	 */
	public QuoteTemplateWrapper getQuoteTemplateWrapper()
	{
		return quoteTemplateWrapper;
	}

	/**
	 * @return the widgetTemplateDescription
	 */
	public HtmlInputText getWidgetTemplateDescription()
	{
		return widgetTemplateDescription;
	}

	/**
	 * @return the widgetTemplateName
	 */
	public HtmlInputText getWidgetTemplateName()
	{
		return widgetTemplateName;
	}

	/**
	 * @return the modifyExistingQuoteAttachment
	 */
	public boolean isModifyExistingQuoteAttachment()
	{
		return modifyExistingQuoteAttachment;
	}

	/**
	 * @return the quoteAttachmentDisabled
	 */
	public boolean isQuoteAttachmentDisabled()
	{
		return quoteAttachmentDisabled;
	}

	/**
	 * @return the quoteAttachmentRequired
	 */
	public boolean isQuoteAttachmentRequired()
	{
		return quoteAttachmentRequired;
	}

	/**
	 * @param fileTypeSelectItemList the fileTypeSelectItemList to set
	 */
	public void setFileTypeSelectItemList(List<SelectItem> fileTypeSelectItemList)
	{
		this.fileTypeSelectItemList = fileTypeSelectItemList;
	}

	/**
	 * @param modifyExistingQuoteAttachment the modifyExistingQuoteAttachment to set
	 */
	public void setModifyExistingQuoteAttachment(boolean modifyExistingQuoteAttachment)
	{
		this.modifyExistingQuoteAttachment = modifyExistingQuoteAttachment;
	}

	/**
	 * @param quoteAttachmentDisabled the quoteAttachmentDisabled to set
	 */
	public void setQuoteAttachmentDisabled(boolean quoteAttachmentDisabled)
	{
		this.quoteAttachmentDisabled = quoteAttachmentDisabled;
	}

	/**
	 * @param quoteAttachmentRequired the quoteAttachmentRequired to set
	 */
	public void setQuoteAttachmentRequired(boolean quoteAttachmentRequired)
	{
		this.quoteAttachmentRequired = quoteAttachmentRequired;
	}

	/**
	 * @param quoteTemplateMasterPojo the quoteTemplateMasterPojo to set
	 */
	public void setQuoteTemplateMasterPojo(QuoteTemplateMasterPojo quoteTemplateMasterPojo)
	{
		this.quoteTemplateMasterPojo = quoteTemplateMasterPojo;
	}

	/**
	 * @param quoteTemplateMasterService the quoteTemplateMasterService to set
	 */
	public void setQuoteTemplateMasterService(QuoteTemplateMasterService quoteTemplateMasterService)
	{
		this.quoteTemplateMasterService = quoteTemplateMasterService;
	}

	/**
	 * @param quoteTemplateWrapper the quoteTemplateWrapper to set
	 */
	public void setQuoteTemplateWrapper(QuoteTemplateWrapper quoteTemplateWrapper)
	{
		this.quoteTemplateWrapper = quoteTemplateWrapper;
	}

	/**
	 * @param widgetTemplateDescription the widgetTemplateDescription to set
	 */
	public void setWidgetTemplateDescription(HtmlInputText widgetTemplateDescription)
	{
		this.widgetTemplateDescription = widgetTemplateDescription;
	}

	/**
	 * @param widgetTemplateName the widgetTemplateName to set
	 */
	public void setWidgetTemplateName(HtmlInputText widgetTemplateName)
	{
		this.widgetTemplateName = widgetTemplateName;
	}
}
